package yxy.game.pm2.gm.url;

import com.alibaba.fastjson.JSONObject;

import yxy.apple.http.server.business.Business;
import yxy.apple.http.server.business.BusinessType;
import yxy.apple.protobuf.Constants;
import yxy.apple.protobuf.external.gateway.Gateway.ReplacedNotification;
import yxy.apple.util.redisson.RedisConnection;
import yxy.banana.socket.NetworkElement;
import yxy.cherry.game.base.bean.GatewayBean;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.gm.result.GMResult;
import yxy.game.pm2.gm.result.GMResult.GMError;

/** GM踢玩家下线 */
@BusinessType(code = "KICKPLAYER")
public class GMkickPlayer implements Business {

	@Override
	public String name() {
		return "gm";
	}

	@Override
	public JSONObject handle(JSONObject request) throws Exception {
		int uid = request.getIntValue("uid");

		RedisConnection redis = RedisManager.getInstance().getConnection(RedisType.REDIS_USER);
		GatewayBean gateway = GatewayBean.find(redis, uid);
		long d = NetworkElement.value(gateway.getSystem(), gateway.getCluster(), gateway.getChannel());
		ReplacedNotification.Builder builder = ReplacedNotification.newBuilder();
		boolean result = MessageEmitterHandler.getInstance().control(null, 0, d, Constants.ControlProto.KickoutNotification, builder.build());
		if (!result) {
			return new GMResult().result(new JSONObject(), GMError.Err_踢人_失败);
		}
		return new GMResult().result(new JSONObject(), GMError.OK);
	}

	@Override
	public Business clone() throws CloneNotSupportedException {
		return (Business) super.clone();
	}

}
